//
//  NuanxinViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "NuanxinViewController.h"

@interface NuanxinViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, assign)NSInteger number;
@end

@implementation NuanxinViewController
- (void)dealloc
{
    [super dealloc];
    [_tableView release];
    [_dataArr release];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //tableView
        [self createTableView];
        
    }
    return self;
    
    
}
- (UITableView *)setWithUrl:(NSString *)str
{//处理数据
    
    [AFNModel GETWithHandler:str completion:^(id result) {
         self.dataArr = [NuanxinModel modelHandlerWithArray:result[0][@"item"]];
        [_tableView reloadData];
    }];
    
    return _tableView;
    
    
}
#warning 上拉加载
- (void)loadnewdata
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?id=NXWPD,FOCUSNXWPD&page=%ld&gv=4.6.9&av=0&proid=ifengnews&os=ios_8.4.1&vt=5&screen=750x1334&publishid=4002&uid=8e6e52d1d80716f49a4d94ed0c4c60e43a8c37da", self.number]  completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [NuanxinModel modelHandlerWithArray:result[0][@"item"]];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    [self.tableView.mj_footer endRefreshing];
    self.number += 1;
}

#warning 下拉刷新
- (void)dadaHandeler
{
    [AFNModel GETWithHandler: @"http://api.iclient.ifeng.com/ClientNews?id=NXWPD,FOCUSNXWPD&page=0&gv=4.6.9&av=0&proid=ifengnews&os=ios_8.4.1&vt=5&screen=750x1334&publishid=4002&uid=8e6e52d1d80716f49a4d94ed0c4c60e43a8c37da"  completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [NuanxinModel modelHandlerWithArray:result[0][@"item"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [self.tableView reloadData];
            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.tableView.mj_header endRefreshing];
        });
        
    }];
    
}



//创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 160) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    //注册cell
    [_tableView registerClass:[NuanxinCell class] forCellReuseIdentifier:@"cell"];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dadaHandeler)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [self.tableView.mj_footer beginRefreshing];
    }
    self.number = 2;

    
    
    //管理内存
    [_tableView release];

}

//tableView协议方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArr.count;
    
}
//cell设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NuanxinModel *nx = _dataArr[indexPath.row];
    NuanxinCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLabel.text = nx.title;
    cell.commentCountLabel.text = nx.comments;
    [cell.imag sd_setImageWithURL:[NSURL URLWithString:nx.thumbnail]];
    cell.timeLabel.text = [nx.updateTime substringWithRange:NSMakeRange(5, 5)];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NuanxinModel *m  =_dataArr[indexPath.row];
    CGFloat h = [NuanxinCell heightWithText:m.title];
    return h + 100;
    
}

#warning cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NuanxinModel *nuan = _dataArr[indexPath.row];
    [self.delegate NuanxinValue:nuan.url];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
